//
//  StoreViewController.m
//  FoodFamily
//
//  Created by dlios on 15-6-22.
//  Copyright (c) 2015年 guoBang. All rights reserved.
//

#import "StoreViewController.h"
#import "FoodCityModle.h"
#import "HotAndMagazineViewController.h"
#import "FoodAboutModle.h"


@interface StoreViewController ()

@end

@implementation StoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor =[UIColor greenColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-fanhui.png"] style:UIBarButtonItemStylePlain target:self action:@selector(laftButtonActiuon)];
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor blackColor]];
    
    self.navigationItem.title = @"美食足迹";
    
    UICollectionViewFlowLayout *collectionVFLayout = [[UICollectionViewFlowLayout alloc]init];
    

    if (WIDTH >= 375) {
        collectionVFLayout.itemSize = CGSizeMake((WIDTH - 40)/2, HEIGHT - 400);
        collectionVFLayout.minimumInteritemSpacing =20;
        collectionVFLayout.minimumLineSpacing = 10;
        collectionVFLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        collectionVFLayout.sectionInset =UIEdgeInsetsMake(15,10, 1,10);
        
       
 
    }else if (WIDTH< 375){
    
        collectionVFLayout.itemSize = CGSizeMake(280,240);
        collectionVFLayout.minimumInteritemSpacing =20;
        collectionVFLayout.minimumLineSpacing = 10;
        collectionVFLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        collectionVFLayout.sectionInset =UIEdgeInsetsMake(15,10, 1,10);

    
    }
     collectionVFLayout.headerReferenceSize = CGSizeMake(WIDTH, 120);
    self.collectV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-20 - 44 - 49) collectionViewLayout:collectionVFLayout];
    self.collectV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectV];
    [self.collectV release];
    self.collectV.delegate = self;
    self.collectV.dataSource = self;
    
    [self.collectV registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
    [self.collectV registerClass:[StoreCollectionViewCell class] forCellWithReuseIdentifier:@"reuser"];
    [self.collectV registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView"];
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载中...";
    [self.HUD show:YES];

    [self getInFo];
    
    // Do any additional setup after loading the view.
}

-(void)getInFo{

    NSString *str = [[self.dic objectForKey:@"last_feedread_time"]substringToIndex:10];
    [AFNGetDataValue getDataAFN:[NSString stringWithFormat:@"http://api.meishixing.com/user/picturelist/footprint/uid=23026&session_id=000012a3048e08151bb0a6fe0608af0f5cf2d0&date=%@",str] block:^(NSMutableDictionary *dataDic){
        self.array = [NSMutableArray array];
        NSMutableArray *array = [dataDic objectForKey:@"result"];
        [self.HUD hide:YES];
        for (NSMutableDictionary *dic in array) {
            
            FoodCityModle *foodCM = [[FoodCityModle alloc]init];
            
            foodCM.date = [dic objectForKey:@"date"];
            
            foodCM.pliclist = [dic objectForKey:@"piclist"];
            
            [self.array addObject:foodCM];
            
        }
        
        
        
        [self.collectV reloadData];
        
    }];
    



}



-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    if (kind == UICollectionElementKindSectionHeader) {
        UICollectionReusableView *headerView  = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerView" forIndexPath:indexPath];
        headerView.backgroundColor = [UIColor whiteColor];
        

        
        
        self.user_image= [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH/2-30, 5, 80, 80)];
        self.user_image.layer.masksToBounds = YES;
        self.user_image.layer.cornerRadius = 40;
        self.user_image.backgroundColor = [UIColor clearColor];
        [headerView addSubview:self.user_image];
        
        NSURL *url = [NSURL URLWithString:[self.dic objectForKey:@"user_image"]];
        [self.user_image sd_setImageWithURL:url];
        [self.user_image release];
        
        
        
        
        self.name = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH/2 - 80, 80, 200, 30)];
        self.name.backgroundColor = [UIColor clearColor];
        self.name.font = [UIFont systemFontOfSize:20];
        self.name.text = [self.dic objectForKey:@"name"];
        [headerView addSubview:self.name];
        self.name.alpha = 0.6;
        [self.name release];
        
        
        //动态确定字体的宽
        UIFont *fnt = [UIFont systemFontOfSize:20];
        CGSize size = [self.name.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil]];
        
         NSInteger length = size.width;

        self.level_title = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH/2 - 80 + length + 2, 87, 40, 15)];
        self.level_title.backgroundColor = [UIColor redColor];
        self.level_title.layer.masksToBounds = YES;
        self.level_title.layer.cornerRadius = 7;
        self.level_title.text = [self.dic objectForKey:@"level_title"];
        self.level_title.textAlignment = NSTextAlignmentCenter;
        self.level_title.textColor = [UIColor yellowColor];
        self.level_title.font = [UIFont systemFontOfSize:12];
        [headerView addSubview:self.level_title];
        [self.level_title release];

        
        
        self.picture_count =[[UILabel alloc]initWithFrame:CGRectMake(WIDTH/2 - 90, 100, 200, 30)];
        
        self.picture_count.backgroundColor = [UIColor clearColor];
        
        self.picture_count.text = [NSString stringWithFormat:@"共%@道美食,%@个积分",[self.dic objectForKey:@"picture_count"],[self.dic objectForKey:@"reputation"]];
        self.picture_count.textAlignment = NSTextAlignmentCenter;
        self.picture_count.alpha = 0.6;
        self.picture_count.font = [UIFont systemFontOfSize:16];
        
        [headerView addSubview:self.picture_count];
        [self.picture_count release];
        
        
        return headerView;
    }else{
        UICollectionReusableView *footView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footerView" forIndexPath:indexPath];
        footView.backgroundColor = [UIColor orangeColor];
        return footView;
        
    }
    
    
    
    
    
    
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (self.array.count != 0) {
        
        FoodCityModle *foodCM = [self.array objectAtIndex:0];
        return foodCM.pliclist.count;
    }
    
    return 0;
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    self.cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuser" forIndexPath:indexPath];
    

    self.cell.backgroundColor =[UIColor clearColor];
    
    
    
     FoodCityModle *foodCM =  [self.array objectAtIndex:0];
    self.cell.dic = [foodCM.pliclist objectAtIndex:indexPath.row];
    
    return self.cell;
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    HotAndMagazineViewController *hotVC = [[HotAndMagazineViewController alloc]init];
    
    [self.navigationController pushViewController:hotVC animated:YES];
    
    
    FoodCityModle *foodCM =  [self.array objectAtIndex:0];
    FoodAboutModle *foodM = [[FoodAboutModle alloc]init];
    foodM.tweet_id =[[foodCM.pliclist objectAtIndex:indexPath.row] objectForKey:@"tweet_id"];
    
    hotVC.hotModle = foodM;
  
    
    [foodM release];
    [hotVC release];

}




-(void)laftButtonActiuon{

    [self.navigationController popViewControllerAnimated:YES];
}
-(void)dealloc{
    [_collectV release];
    [_cell release];
    [_user_image release];
    [_name release];
    [_level_title release];
    [_picture_count release];
    [_dic release];
   [_array release];
    [_HUD release];
    [super dealloc];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    self.view.backgroundColor =[UIColor greenColor];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
